package com.atguigu.aiproject.config;

import com.atguigu.aiproject.interceptor.JwtHandshakeInterceptor;
import com.atguigu.aiproject.websocket.handler.ChatWebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private  ChatWebSocketHandler chatWebSocketHandler;
    @Autowired
    private JwtHandshakeInterceptor jwtHandshakeInterceptor;


    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(chatWebSocketHandler, "/ws/chat")//将处理器映射到路径
                .setAllowedOrigins("*")//允许跨域
                .addInterceptors(jwtHandshakeInterceptor);//添加自定义握手拦截器
    }
}
